﻿Imports System.Windows.Threading
Imports System.Collections.ObjectModel
Imports System.Threading.Tasks
Imports System.Threading
Namespace Statistics
    Public Class CompanyStats
        'Umsatzübersicht (tag, woche, monat jahr)

        Private _dispatcher As Dispatcher
        Private _loader As IDatabase
        Private _usr As User

        Public Sub New(dp As Dispatcher, loader As IDatabase, usr As User)
            _dispatcher = dp
            _loader = loader
            _usr = usr
        End Sub

        ''' <summary>
        '''  Creates the sales list from the given start- and enddate. The list is sorted by the biggest amount
        ''' </summary>
        ''' <param name="startdate">From where you want to calculate the list</param>
        ''' <param name="enddate">To where you want to calculate the list</param>
        ''' <remarks></remarks>
        Public Sub GetCustomerSalesListAsync(startdate As Date, enddate As Date)
            Task.Factory.StartNew(Sub()
                                      GetCustomerSalesList(startdate, enddate)
                                  End Sub)
        End Sub

        ''' <summary>
        ''' Creates the sales list from the 1st january to the 31st december of the current year. The list is sorted by the biggest amount
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub GetCustomerSalesListCurrentYearAsync()
            Task.Factory.StartNew(Sub()
                                      GetCustomerSalesList(New Date(Date.Today.Year, 1, 1), New Date(Date.Today.Year, 12, 31))
                                  End Sub)
        End Sub

        Private Sub GetCustomerSalesList(startdate As Date, enddate As Date)
            '### Orders ###'
            Dim orders As Collection(Of OrderPreviewModel) = _loader.GetOrdersInInterval(startdate, enddate, _usr)

            '### Addition ###'
            Dim output As New List(Of CustomerValueModel)
            Dim m As New Mutex

            For Each o In orders
                Dim item As New CustomerValueModel(o.CustomerID, o.CustomerName, o.Value())
                If output.Contains(item) Then
                    Dim existingitem As CustomerValueModel = output.Item(output.IndexOf(item))
                    existingitem.Value += item.Value
                Else
                    output.Add(item)
                End If
            Next

            Dim complete As New Money(0)
            For Each item In output
                complete += item.Value
            Next

            For Each item In output
                item.CalcPercent(complete.Amount)
            Next

            Dim result As New Collection(Of CustomerValueModel)
            Dim sort As New SortCustomerValueModelByValue
            output.Sort(sort)

            For Each item In output
                result.Add(item)
            Next

            _dispatcher.BeginInvoke(Sub()
                                        RaiseEvent GetCustomerSalesListCompleted(result)
                                    End Sub)
        End Sub

        Private Sub DeleteItemsFromCollection(ByRef basecollection As Collection(Of Order), ItemsToDelete As Collection(Of Order))
            For Each item In ItemsToDelete
                basecollection.Remove(item)
            Next
        End Sub

        Public Shared Function IsDateBetween(startdate As Date, enddate As Date, yourdate As Date) As Boolean
            If DateTime.Compare(startdate, yourdate) <= 0 Then
                If DateTime.Compare(enddate, yourdate) >= 0 Then
                    Return True
                End If
            End If
            Return False
        End Function

        Public Sub GetMonthlySalesAsync(startdate As Date, enddate As Date)
            Task.Factory.StartNew(Sub()
                                      GetMonthlySales(startdate, enddate)
                                  End Sub)
        End Sub

        Private Sub GetMonthlySales(startdate As Date, enddate As Date)
            Dim output As New Collection(Of DateValueModel)
            ' Just select orders and credits
            Dim orders = From o In _loader.GetOrdersInInterval(startdate, enddate, _usr) Where (o.OrderTyp = "1" OrElse o.OrderTyp = "3")

            '### Prepare output ###'
            Dim tempstart As Date = New Date(startdate.Year, startdate.Month, 1)
            Dim tempend As Date = enddate : enddate.AddMonths(1) : tempend = New Date(tempend.Year, tempend.Month, 1) : tempend.AddDays(-1)
            Dim currentdate As Date = tempstart
            Do While currentdate <= tempend
                Dim item As New DateValueModel
                Dim d As New Datum(currentdate)
                item.Date_ = d
                item.Value = New Money(0)
                output.Add(item)
                currentdate = currentdate.AddMonths(1)
            Loop

            '### Calcualte output ###'
            For Each o In orders
                ' Credits get subtracted
                If o.OrderTyp = "3" Then
                    o.Value = o.Value * (-1)
                End If

                Dim item As New DateValueModel
                Dim orderdate = New Datum(o.Date_)
                Dim d As New Datum(1, orderdate.Monat, orderdate.Jahr)
                item.Date_ = d
                If output.Contains(item) Then
                    item = output.Item(output.IndexOf(item))
                    item.Value += o.Value()
                Else
                    item.Value = o.Value()
                    output.Add(item)
                End If
            Next

            _dispatcher.BeginInvoke(Sub()
                                        RaiseEvent GetMonthlySalesListCompleted(output)
                                    End Sub)
        End Sub
#Region "Events"
        Public Event GetCustomerSalesListCompleted(result As Collection(Of CustomerValueModel))
        Public Event GetMonthlySalesListCompleted(result As Collection(Of DateValueModel))
#End Region
    End Class
End Namespace
